package com.pawernode.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "my")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RabbitConfig {

    private String exchangeName;
    private String queueName;

    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    @Bean
    public Queue queue() {
        // 设置队列属性
        // 1，new Queue() 的方法
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 15000); // 消息过期时间
        return new Queue(queueName, true, false, false, arguments);
        // 方式2 建造者
//        return QueueBuilder
//                .durable(queueName)
//                .withArguments(arguments)
//                .build();
    }

    @Bean
    public Binding binding(DirectExchange directExchange, Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("info");
    }
}
